package com.xnew.resource.service;

import com.alibaba.fastjson.JSONArray;
import com.xnew.bean.common.SysUserInfo;
import com.xnew.resource.bean.AuResource;
import com.xnew.resource.mapper.AuResourceMapper;
import com.xnew.utils.IdUtils;
import com.xnew.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Component
@Transactional(rollbackFor = Exception.class)
public class AuResourceService {
	
	@Autowired
	private AuResourceMapper resourceMapper;

	/**
	 * 查询资源信息，构建资源树结构
	 * */
	public List<AuResource> createResourceTree()  throws Exception{
		// 原始的数据
		List<AuResource> resourceList = resourceMapper.selectAll();
		// 最后的结果
		List<AuResource> resourceAllList = new ArrayList<AuResource>();
		// 会员档案审批首页待办提醒list
		ArrayList<AuResource> auResourcesMember = new ArrayList<>();
		// 调解平台审批首页待办提醒list
		ArrayList<AuResource> auResourcesMediat = new ArrayList<>();
		// 固定收益审批首页待办提醒list
		ArrayList<AuResource> auResourcesFixIncome = new ArrayList<>();
		// 先找到所有的一级菜单
		for (int i = 0; i < resourceList.size(); i++) {
			resourceList.get(i).setStatus(false);
			// 一级菜单parentId为0
			if ("0".equals(resourceList.get(i).getParentId())) {
				resourceList.get(i).setStatus(true);
				resourceAllList.add(resourceList.get(i));
			}
		}

		// 为一级菜单设置子菜单，getChild是递归调用的
		for (AuResource resource : resourceAllList) {
			resource.setChildren(this.getChild(resource.getMenuId(), resourceList));
		}
		return resourceAllList;
	}
	
	/**
	 * @方法名：createRoleResourceTree
	 * @功能说明：角色资源树
	 * @author yangyuan3
	 * @date  2019年9月10日 下午5:15:44
	 * @param user
	 * @return
	 * @throws Exception
	 */
	public List<AuResource> createRoleResourceTree(SysUserInfo user)  throws Exception{
		// 原始的数据
		List<AuResource> resourceList = resourceMapper.selectRoleResourceTree(user.getUserInfoNo());
		// 最后的结果
		List<AuResource> resourceAllList = new ArrayList<AuResource>();
		// 先找到所有的一级菜单
		for (int i = 0; i < resourceList.size(); i++) {
			resourceList.get(i).setStatus(false);
			// 一级菜单parentId为0
			if ("0".equals(resourceList.get(i).getParentId())) {
				resourceList.get(i).setStatus(true);
				resourceAllList.add(resourceList.get(i));
			}
		}
		// 为一级菜单设置子菜单，getChild是递归调用的
		for (AuResource resource : resourceAllList) {
			resource.setChildren(this.getChild(resource.getMenuId(), resourceList));
		}
		return resourceAllList;
	}

	/**
	 * 递归查找子菜单及资源
	 * @return
	 */
	private List<AuResource> getChild(String id, List<AuResource> rootMenu)  throws Exception{
		List<AuResource> childList = new ArrayList<>();
		for (AuResource resource : rootMenu) {
			if (!resource.getStatus()) {
				if (resource.getParentId().equals(id)) {
					resource.setStatus(true);
					childList.add(resource);
				}
			}
		}
		for (AuResource resource : childList) {
			if("module".equals(resource.getArsType()) || "url".equals(resource.getArsType())){
				resource.setChildren(getChild(resource.getMenuId(), rootMenu));
			}
		}
		if (childList.size() == 0) {
			return null;
		}
		return childList;
	}

	public AuResource queryResourceById(String id)  throws Exception{
		return resourceMapper.selectByPrimaryKey(id);
	}

	public void deleteById(String id)  throws Exception{
		resourceMapper.deleteByPrimaryKey(id);
	}

	public void addOrUpdateResource(AuResource resource)  throws Exception{
		if(StringUtils.isBlank(resource.getArsId())){
			addResource(resource);
		}else{
			updateResource(resource);
		}
	}

	private void updateResource(AuResource resource)  throws Exception{
		resourceMapper.updateByPrimaryKey(resource);
	}

	private void addResource(AuResource resource)  throws Exception{
		resource.setArsId(IdUtils.getIncreaseIdByNanoTime());
		resourceMapper.insert(resource);
	}

	public void deleteRoleResource(String roleId) throws Exception{
		resourceMapper.deleteRoleResource(roleId);
	}
	
	public List<AuResource> selectRoleResource(String roleId) throws Exception{
		List<AuResource> list = resourceMapper.selectRoleResource(roleId);
		return list;
	}

	/**
	 * @方法名：selectAllotRoleResource
	 * @功能说明：反显已分配角色资源
	 * @author yangyuan3
	 * @date  2019年9月10日 下午5:43:36
	 * @param roleId
	 * @return
	 */
	public List<AuResource> selectAllotRoleResource(String roleId) {
		List<AuResource> list = resourceMapper.selectAllotRoleResource(roleId);
		return list;
	}

	public void insertRoleResource(String roleId, JSONArray visibleResourceIds,
								   String auId) throws Exception {
		// 可见资源
		this.deleteRoleResource(roleId);
		if (null != visibleResourceIds && visibleResourceIds.size() != 0) {
			for (int i = 0; i < visibleResourceIds.size(); i++) {
				String arrId = IdUtils.getIncreaseIdByCurrentTimeMillis();
				String arsId = visibleResourceIds.getString(i);
				resourceMapper.insertRoleResource(arrId, roleId, arsId, auId);
			}
		}
	}
}
